﻿window.ArthurGraus = window.ArthurGraus || {};

var spoe;
$(document).ready(function () {
    spoe = new ArthurGraus.SharePointObjectExplorer();
    spoe.initialize();
});


ArthurGraus.SharePointObjectExplorer = function () {
    var self = this;
    var listSaver = new Array();
    var headerSaver = new Array();
    var listRecoder = new Array();
    var listRecoderMain = new Array();
    var FileDirRef = new Array();
    var ItemWebParts = new Array();
    var hostWebSubweb = new Array();
    var FileDirRefSitePage = "";
    var getFiles = null;
    var getFolders = null;
    var waitDialog = null;
    var itemCollection = new Array();
    var folderCollection = new Array();
    var SiteName = "";
    self.hostWebUrl = "";
    self.appWebContext = null;
    self.hostWebContext = null;
    self.hostWeb = null;
    self.itemCollection = null;
    var Item = null;
    var listDetails = new Array("Items", "Properties", "Fields", "EventReceivers", "ContentTypes", "Views", "WorkflowAssociations");
    var propertyDetails = new Array("Fixed Properties", "Properties Bag");
    var groupDetails = new Array("Users", "Properties");
    var userDetails = new Array("Groups", "Properties");
    var libraryDetails = new Array("Documents", "Properties", "Fields", "EventReceivers", "ContentTypes", "Views", "WorkflowAssociations");
    var sitePageDetails = new Array("Properties", "WebParts");
    var camlQuery = new SP.CamlQuery.createAllItemsQuery();
    self.initialize = function () {
        $("#LISTS").click(self.LISTS_Click);
        $("#PROPERTIES").click(self.PROPERTIES_Click);
        $("#LIBRARIES").click(self.LIBRARIES_Click);
        $("#SITEPAGES").click(self.SITEPAGES_Click);
        $("#APPS").click(self.APPS_Click);
        $("#CONTENTTYPES").click(self.CONTENT_TYPES_Click);
        $("#FIELDS").click(self.FIELDS_Click);
        $("#SUBSITES").click(self.SUBSITES_Click);
        $("#GROUPS").click(self.GROUPS_Click);
        $("#ALLUSERS").click(self.ALLUSERS_Click);
        $("#buttonBack").click(self.buttonBack_Click);
        $("#listDynamic").click(self.listDynamicClick);
        $("#divDynamic").hide();
        $("#tableDynamic").hide();
        $("#buttonMain").click(self.buttonMain_Click);
        self.hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
        self.appWebContext = new SP.ClientContext.get_current();
        self.hostWebContext = new SP.AppContextSite(self.appWebContext, self.hostWebUrl);
        self.hostWeb = self.hostWebContext.get_web();
        Item = self.hostWeb;
        self.appWebContext.load(Item);
        self.appWebContext.executeQueryAsync(self.onGetSiteName, self.onError);
    }
    self.onGetSiteName = function () {
        SiteName = Item.get_title();
        self.HeaderN();
    }

    self.Wait = function () {
        waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose(SP.Res.dialogLoading15);
    }

    self.CancelWait = function () {
        waitDialog.close();
    }

    self.getAncestor = function (node, tagName) {
        tagName = tagName.toUpperCase();
        while (node) {
            if (node.nodeType == 1 && node.nodeName == tagName) {
                return node;
            }
            node = node.parentNode;
        }
        return null;
    }
    self.HeaderN = function () {
        var Header = "";
        for (x in headerSaver) {
            if (x == 0) {
                Header += SiteName;
                Header += "-" + headerSaver[x];
            }
            else
                Header += "-" + headerSaver[x];
        }
        if (headerSaver.length == 0)
            Header = SiteName;
        $("#headerDynamic").html(Header);
        $("#HeaderMain").html(Header);
    }

    self.listDynamicClick = function (e) {
        var a = self.getAncestor(e.target, "LI");
        if (a != null && a.nodeName == "LI") {
            self.Wait();
            listSaver.push(a.id);
            headerSaver.push(a.id);
            if (listSaver[0] == "PROPERTIES") {
                if (listSaver[1] == "Fixed Properties") {
                    Item = self.hostWeb;
                    self.appWebContext.load(Item);
                    self.appWebContext.executeQueryAsync(self.onGetProperties, self.onError);
                }
                if (listSaver[1] == "Properties Bag") {
                    Item = self.hostWeb.get_allProperties();
                    self.appWebContext.load(Item);
                    self.appWebContext.executeQueryAsync(self.onGetItemProperties, self.onError);
                }

            }
            else if (listSaver[0] == "APPS") {
                if (listSaver.length == 2) {
                    self.onGetApps();
                }
            }
            else if (listSaver[0] == "LISTS" || listSaver[0] == "LIBRARIES") {
                if (listSaver.length == 2) {
                    $("#listDynamic").empty();
                    listRecoder = [];
                    if (listSaver[0] == "LISTS") {
                        for (x in listDetails) {
                            itemHTML = '<li id="' + listDetails[x] + '"><div class="soe-main-list-image"><img src="../Images/' + listDetails[x] + '.png" alt="Icon" /></div>' +
                            '<div class="soe-main-list-title" >' + listDetails[x] + '</div></li>';
                            $('#listDynamic').append($('<li></li>').html(itemHTML));
                            listRecoder.push(itemHTML);
                        }
                    }
                    else {
                        for (x in libraryDetails) {
                            itemHTML = '<li id="' + libraryDetails[x] + '"><div class="soe-main-list-image"><img src="../Images/' + libraryDetails[x] + '.png" alt="Library" /></div>' +
                            '<div class="soe-main-list-title" >' + libraryDetails[x] + '</div></li>';
                            $('#listDynamic').append($('<li></li>').html(itemHTML));
                            listRecoder.push(itemHTML);
                        }
                    }
                    listRecoderMain.push(listRecoder);
                    self.CancelWait();
                    self.HeaderN();
                }
                else if (listSaver.length > 2) {
                    if (listSaver[2] == "Properties") {
                        if (listSaver.length == 3) {
                            Item = self.hostWeb.get_lists().getByTitle(listSaver[1]);
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.onGetProperties, self.onError);
                        }
                    }
                    if (listSaver[2] == "Fields") {
                        if (listSaver.length == 3) {
                            self.itemCollection = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_fields();
                            self.appWebContext.load(self.itemCollection);
                            self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
                        }
                        if (listSaver.length == 4) {
                            Item = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_fields().getByTitle(listSaver[3]);
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.onGetProperties, self.onError);
                        }
                    }
                    if (listSaver[2] == "EventReceivers") {
                        if (listSaver.length == 3) {
                            self.itemCollection = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_eventReceivers();
                            self.appWebContext.load(self.itemCollection);
                            self.appWebContext.executeQueryAsync(self.onGetEventReceiversSucceeded, self.onError);
                        }
                        if (listSaver.length == 4) {
                            Item = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_eventReceivers().getById(listSaver[3]);
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.GetEventReceiversDetails, self.onError);
                        }
                    }
                    if (listSaver[2] == "Items") {
                        if (listSaver.length == 3) {
                            self.itemCollection = self.hostWeb.get_lists().getByTitle(listSaver[1]).getItems(camlQuery);
                            self.appWebContext.load(self.itemCollection);
                            self.appWebContext.executeQueryAsync(self.onGetItemSucceeded, self.onError);

                        }
                        if (listSaver.length == 4) {
                            Item = self.hostWeb.get_lists().getByTitle(listSaver[1]).getItemById(listSaver[3]);
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.onGetItemProperties, self.onError);

                        }
                    }

                    if (listSaver[2] == "Documents") {
                        if (listSaver.length == 3) {
                            self.itemCollection = self.hostWeb.get_lists().getByTitle(listSaver[1]).getItems(camlQuery);
                            self.appWebContext.load(self.itemCollection);
                            self.appWebContext.executeQueryAsync(self.onGetItemFileDirRef, self.onError);
                        }
                        if (listSaver.length > 3) {
                            Item = self.hostWeb.get_lists().getByTitle(listSaver[1]).getItemById(listSaver[listSaver.length - 1]);
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.onGetFileDirRef, self.onError);
                        }

                    }

                    if (listSaver[2] == "ContentTypes") {
                        if (listSaver.length == 3) {
                            self.itemCollection = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_contentTypes();
                            self.appWebContext.load(self.itemCollection);
                            self.appWebContext.executeQueryAsync(self.onGetContentTypeSucceeded, self.onError);
                        }
                        if (listSaver.length == 4) {
                            Item = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_contentTypes().getById(listSaver[3]);
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.GetContentDetails, self.onError);
                        }

                    }
                    if (listSaver[2] == "Views") {
                        if (listSaver.length == 3) {
                            self.itemCollection = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_views();
                            self.appWebContext.load(self.itemCollection);
                            self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
                        }
                        if (listSaver.length == 4) {
                            Item = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_views().getByTitle(listSaver[3]);
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.onGetProperties, self.onError);
                        }
                    }
                    if (listSaver[2] == "WorkflowAssociations") {
                        if (listSaver.length == 3) {
                            self.itemCollection = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_workflowAssociations();
                            self.appWebContext.load(self.itemCollection);
                            self.appWebContext.executeQueryAsync(self.onGetContentTypeSucceeded, self.onError);
                        }
                        if (listSaver.length == 4) {
                            Item = self.hostWeb.get_lists().getByTitle(listSaver[1]).get_workflowAssociations().getById(listSaver[3]);
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.GetContentDetails, self.onError);
                        }
                    }
                }
                else {
                    listSaver.pop();
                    headerSaver.pop();
                }
            }
            else if (listSaver[0] == "SITEPAGES") {
                if (listSaver.length == 2) {
                    $("#listDynamic").empty();
                    listRecoder = [];
                    Item = self.hostWeb.get_lists().getByTitle("Site Pages").getItemById(listSaver[1]);
                    self.appWebContext.load(Item);
                    self.appWebContext.executeQueryAsync(self.onGetSitePage, self.onError);
                    for (x in sitePageDetails) {
                        itemHTML = '<li id="' + sitePageDetails[x] + '"><div class="soe-main-list-image"><img src="../Images/' + sitePageDetails[x] + '.png" alt="Library" /></div>' +
                        '<div class="soe-main-list-title" >' + sitePageDetails[x] + '</div></li>';
                        $('#listDynamic').append($('<li></li>').html(itemHTML));
                        listRecoder.push(itemHTML);
                    }
                    listRecoderMain.push(listRecoder);
                }
                if (listSaver.length == 3) {
                    if (listSaver[2] == "Properties") {
                        Item = self.hostWeb.get_lists().getByTitle("Site Pages").getItemById(listSaver[1]);
                        self.appWebContext.load(Item);
                        self.appWebContext.executeQueryAsync(self.onGetItemProperties, self.onError);
                    }
                    if (listSaver[2] == "WebParts") {

                        var oFile = self.hostWeb.getFileByServerRelativeUrl(FileDirRefSitePage);
                        var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
                        self.itemCollection = limitedWebPartManager.get_webParts();
                        self.appWebContext.load(self.itemCollection);
                        self.appWebContext.executeQueryAsync(self.onGetWebPartSucceeded, self.onError);
                    }
                }
                if (listSaver.length == 4) {
                    for (var count in ItemWebParts) {
                        if (ItemWebParts[count].get_title() == listSaver[3]) {
                            Item = ItemWebParts[count].get_properties();
                            self.appWebContext.load(Item);
                            self.appWebContext.executeQueryAsync(self.onGetItemProperties, self.onError);
                        }
                    }
                }

            }
            else if (listSaver[0] == "FIELDS") {
                if (listSaver.length == 2) {
                    Item = self.hostWeb.get_fields().getByTitle(listSaver[1]);
                    self.appWebContext.load(Item);
                    self.appWebContext.executeQueryAsync(self.onGetProperties, self.onError);
                }


            }
            else if (listSaver[0] == "CONTENTTYPES") {
                if (listSaver.length == 2) {
                    Item = self.hostWeb.get_contentTypes().getById(listSaver[1]);
                    self.appWebContext.load(Item);
                    self.appWebContext.executeQueryAsync(self.GetContentDetails, self.onError);

                }

            }
            else if (listSaver[0] == "SUBSITES") {
                self.itemCollection = self.hostWeb.get_webs();
                self.appWebContext.load(self.itemCollection);
                self.appWebContext.executeQueryAsync(self.onGetSubSite, self.onError);

            }
            else if (listSaver[0] == "GROUPS" || listSaver[0] == "ALLUSERS") {
                if (listSaver[listSaver.length - 2] == "Groups" || listSaver[listSaver.length - 2] == "GROUPS") {
                    $("#listDynamic").empty();
                    listRecoder = [];
                    for (x in groupDetails) {
                        itemHTML = '<li id="' + groupDetails[x] + '"><div class="soe-main-list-image"><img src="../Images/' + groupDetails[x] + '.png" alt="Icon" /></div>' +
                        '<div class="soe-main-list-title" >' + groupDetails[x] + '</div></li>';
                        $('#listDynamic').append($('<li></li>').html(itemHTML));
                        listRecoder.push(itemHTML);
                    }
                    Item = self.hostWeb.get_siteGroups().getById(listSaver[listSaver.length - 1]);
                    self.appWebContext.load(Item);
                    self.appWebContext.executeQueryAsync(self.onGetTitle, self.onError);
                    listRecoderMain.push(listRecoder);
                }
                if (listSaver[listSaver.length - 2] == "Users" || listSaver[listSaver.length - 2] == "ALLUSERS") {
                    $("#listDynamic").empty();
                    listRecoder = [];
                    for (x in userDetails) {
                        itemHTML = '<li id="' + userDetails[x] + '"><div class="soe-main-list-image"><img src="../Images/' + userDetails[x] + '.png" alt="Icon" /></div>' +
                        '<div class="soe-main-list-title" >' + userDetails[x] + '</div></li>';
                        $('#listDynamic').append($('<li></li>').html(itemHTML));
                        listRecoder.push(itemHTML);
                    }
                    Item = self.hostWeb.get_siteUsers().getById(listSaver[listSaver.length - 1]);
                    self.appWebContext.load(Item);
                    self.appWebContext.executeQueryAsync(self.onGetTitle, self.onError);
                    listRecoderMain.push(listRecoder);

                }
                if (listSaver[listSaver.length - 1] == 'Properties') {
                    if (listSaver[listSaver.length - 3] == "Users" || listSaver[listSaver.length - 3] == "ALLUSERS") {
                        Item = self.hostWeb.get_siteUsers().getById(listSaver[listSaver.length - 2]);
                        self.appWebContext.load(Item);
                        self.appWebContext.executeQueryAsync(self.onGetProperties, self.onError);
                    }
                    if (listSaver[listSaver.length - 3] == "Groups" || listSaver[listSaver.length - 3] == "GROUPS") {
                        Item = self.hostWeb.get_siteGroups().getById(listSaver[listSaver.length - 2]);
                        self.appWebContext.load(Item);
                        self.appWebContext.executeQueryAsync(self.onGetProperties, self.onError);
                    }
                }
                if (listSaver[listSaver.length - 1] == 'Users') {
                    self.itemCollection = self.hostWeb.get_siteGroups().getById(listSaver[listSaver.length - 2]).get_users();
                    self.appWebContext.load(self.itemCollection);
                    self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
                }
                if (listSaver[listSaver.length - 1] == 'Groups') {
                    self.itemCollection = self.hostWeb.get_siteUsers().getById(listSaver[listSaver.length - 2]);
                    self.appWebContext.load(self.itemCollection);
                    self.appWebContext.executeQueryAsync(self.onGetGroupSucceeded, self.onError);
                }
            }
            else {
                headerSaver.pop();
                listSaver.pop();
            }
        }
    }
    self.htmlEscape = function (str) {
        return String(str)
                .replace(/&/g, '&amp;')
                .replace(/"/g, '&quot;')
                .replace(/'/g, '&#39;')
                .replace(/</g, '&lt;')
                .replace(/>/g, '&gt;')
                .replace(/ /g, '&nbsp;')
                .replace(/\n/g, '<br />');
    }

    self.onGetTitle = function () {
        headerSaver[headerSaver.length - 1] = Item.get_title();
        self.CancelWait();
        self.HeaderN();
    }

    self.onGetGroupSucceeded = function () {
        self.itemCollection = self.itemCollection.get_groups();
        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
    }

    self.onGetSubSite = function () {
        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        while (itemEnumerator.moveNext()) {
            var oItem = itemEnumerator.get_current();
            if (oItem.get_title() == listSaver[1]) {
                hostWebSubweb.push(self.hostWeb);
                self.hostWeb = oItem;
                $("#divMain").show();
                $("#buttonMain")[0].src = "../Images/BackIcon.png";
                $("#divDynamic").hide();
                listSaver = [];
            }
        }
        self.CancelWait();
        self.HeaderN();
    }

    self.onGetFileDirRef = function () {
        var Ref = Item.get_fieldValues().FileRef;
        FileDirRef.push(Ref);
        headerSaver[headerSaver.length - 1] = Item.get_fieldValues().FileLeafRef;
        if (Item.get_fieldValues().FileLeafRef.indexOf(".") == -1) {
            self.onGetItemFileDirRef();
        }
        else {
            self.onGetItemProperties();
        }
    }
    self.onGetSitePage = function () {
        FileDirRefSitePage = Item.get_fieldValues().FileRef;
        headerSaver[headerSaver.length - 1] = Item.get_fieldValues().FileLeafRef;
        self.HeaderN();
        self.CancelWait();
    }

    self.onGetApps = function () {
        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        while (itemEnumerator.moveNext()) {
            var oItem = itemEnumerator.get_current();
            if (oItem.get_title() == listSaver[1]) {
                Item = oItem;
                self.onGetProperties();
            }
        }

    }

    self.onGetProperties = function () {
        var itemHTML = '';
        var currentValue;
        var currentProperty;
        var propertyValue;
        $("#listDynamic").hide();
        $("#tableDynamic").show();
        $("#tableDynamic").empty();
        $("#tableDynamic").append($('<tr></tr>').html('<th>Property</th><th class="emptyCollumn"> </th><th>Value</th>'));
        listRecoderMain.push(listRecoder);
        itemCollection = [];
        for (var key in Item) {
            try {
                if (typeof Item[key] == "function" && key.indexOf("get_") != -1) {
                    currentProperty = key.substring(4);
                    currentProperty = currentProperty.substring(0, 1).toUpperCase() + currentProperty.substring(1);
                    eval("currentValue= Item." + key + "();");
                    if (String(currentValue)) {
                        if (String(currentValue).indexOf("<") == 0) {
                            propertyValue = { "currentProperty": currentProperty, "currentValue": self.htmlEscape(vkbeautify.xml(currentValue)) };
                        }
                        else if (JSON.stringify(currentValue).indexOf("{") == 0) {
                            propertyValue = { "currentProperty": currentProperty, "currentValue": self.htmlEscape(vkbeautify.json(currentValue)) };
                        }
                        else
                            propertyValue = { "currentProperty": currentProperty, "currentValue": self.htmlEscape(currentValue) };
                        itemCollection.push(propertyValue);
                    }
                }
            }
            catch (err) {

            }
        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.currentProperty).toLowerCase(), nameB = String(b.currentProperty).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });

        for (var count in itemCollection) {
            itemHTML = '<td valign="top">' + itemCollection[count].currentProperty + '</td><td class="emptyCollumn"> </td><td>' + itemCollection[count].currentValue + '</td>';
            $("#tableDynamic").append($('<tr></tr>').html(itemHTML));
        }
        self.CancelWait();
        self.HeaderN();
    }

    self.onGetItemFileDirRef = function () {
        $("#divMain").hide();
        $("#divDynamic").show();
        if (FileDirRef.length == 0) {
            var itemEnumerator = self.itemCollection.getEnumerator();
            while (itemEnumerator.moveNext()) {
                var oItem = itemEnumerator.get_current();
                if (Ref == null)
                    Ref = oItem.get_fieldValues().FileDirRef;
                else {
                    if (Ref.length > oItem.get_fieldValues().FileDirRef.length)
                        Ref = oItem.get_fieldValues().FileDirRef;
                }
            }
            FileDirRef.push(Ref);
        }
        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        var Ref = null;
        $("#listDynamic").empty();
        listRecoder = [];
        itemCollection = [];
        folderCollection = [];
        while (itemEnumerator.moveNext()) {
            oItem = itemEnumerator.get_current();
            if (oItem.get_fieldValues().FileLeafRef.indexOf(".") == -1) {
                folderCollection.push(oItem);
            }
            else {
                itemCollection.push(oItem);
            }
        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.get_fieldValues().FileLeafRef).toLowerCase(), nameB = String(b.get_fieldValues().FileLeafRef).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        folderCollection.sort(function (a, b) {
            var nameA = a.get_fieldValues().FileLeafRef.toLowerCase(), nameB = b.get_fieldValues().FileLeafRef.toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        for (var count in folderCollection) {
            if (folderCollection[count].get_fieldValues().FileDirRef == FileDirRef[FileDirRef.length - 1]) {
                itemHTML = '<li id="' + folderCollection[count].get_id() + '"><div class="soe-main-list-image"><img src="../Images/folder.png" alt="folder" /></div>' +
                    '<div class="soe-main-list-title">' + folderCollection[count].get_fieldValues().FileLeafRef + '</div></li>';
                $('#listDynamic').append($('<li></li>').html(itemHTML));
                listRecoder.push(itemHTML);
            }
        }
        for (var count in itemCollection) {
            if (listSaver[0] == "SITEPAGES") {
                itemHTML = '<li id="' + itemCollection[count].get_id() + '"><div class="soe-main-list-image"><img src="../Images/SitePage.png" alt="SitePages" /></div>' +
                   '<div class="soe-main-list-title">' + itemCollection[count].get_fieldValues().FileLeafRef + '</div></li>';
                $('#listDynamic').append($('<li></li>').html(itemHTML));
                listRecoder.push(itemHTML);
            }
            else if (itemCollection[count].get_fieldValues().FileDirRef == FileDirRef[FileDirRef.length - 1]) {
                itemHTML = '<li id="' + itemCollection[count].get_id() + '"><div class="soe-main-list-image"><img src="../Images/files.png" alt="files" /></div>' +
                       '<div class="soe-main-list-title">' + itemCollection[count].get_fieldValues().FileLeafRef + '</div></li>';
                $('#listDynamic').append($('<li></li>').html(itemHTML));
                listRecoder.push(itemHTML);
            }

        }

        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();
    }

    self.onGetItemProperties = function () {
        var itemHTML = '';
        var currentValue;
        var currentProperty;
        var ItemProperty = Item.get_fieldValues();
        $("#listDynamic").hide();
        $("#tableDynamic").show();
        $("#tableDynamic").empty();
        $("#tableDynamic").append($('<tr></tr>').html('<th>Property</th><th class="emptyCollumn"> </th><th>Value</th>'));
        if (listSaver[0] != "PROPERTIES" && listSaver[0] != "LIBRARIES" && listSaver[0] != "SITEPAGES") {
            headerSaver[headerSaver.length - 1] = Item.get_item('Title');
        }
        if (listSaver[0] == "LIBRARIES") {
            headerSaver[headerSaver.length - 1] = Item.get_fieldValues().FileLeafRef;
        }
        listRecoderMain.push(listRecoder);

        itemCollection = [];
        for (var key in ItemProperty) {
            try {
                currentProperty = key.substring(0);
                currentProperty = currentProperty.substring(0, 1).toUpperCase() + currentProperty.substring(1);
                eval("currentValue= ItemProperty." + key + ";");
                if (typeof currentValue != "object" && String(currentValue)) {
                    if (String(currentValue).indexOf("<") == 0) {
                        var propertyValue = { "currentProperty": currentProperty, "currentValue": self.htmlEscape(vkbeautify.xml(currentValue)) };
                    }
                    else if (JSON.stringify(currentValue).indexOf("{") == 0) {
                        var propertyValue = { "currentProperty": currentProperty, "currentValue": self.htmlEscape(vkbeautify.json(currentValue)) };
                    }
                    else
                        var propertyValue = { "currentProperty": currentProperty, "currentValue": self.htmlEscape(currentValue) };
                    itemCollection.push(propertyValue);
                }
                else {
                    for (var Objectkey in ItemProperty[key]) {
                        try {
                            if (typeof ItemProperty[key][Objectkey] == "function" && Objectkey.indexOf("get_") != -1) {
                                var currentPropertyDetail = currentProperty + "." + Objectkey.substring(4);
                                eval("currentValue= ItemProperty." + key + "." + Objectkey + "();");
                                if (String(currentValue)) {
                                    if (String(currentValue).indexOf("<") == 0)
                                        var propertyValue = { "currentProperty": currentPropertyDetail, "currentValue": self.htmlEscape(vkbeautify.xml(currentValue)) };
                                    else if (JSON.stringify(currentValue).indexOf("{") == 0) {
                                        var propertyValue = { "currentProperty": currentPropertyDetail, "currentValue": self.htmlEscape(vkbeautify.json(currentValue)) };
                                        alert(vkbeautify.xml(currentValue));
                                    }
                                    else
                                        var propertyValue = { "currentProperty": currentPropertyDetail, "currentValue": currentValue };
                                    itemCollection.push(propertyValue);
                                }
                            }
                        }
                        catch (err) {

                        }
                    }
                }
            }
            catch (err) {

            }
        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.currentProperty).toLowerCase(), nameB = String(b.currentProperty).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });

        for (var count in itemCollection) {
            itemHTML = '<td valign="top">' + itemCollection[count].currentProperty + '</td><td class="emptyCollumn"> </td><td>' + itemCollection[count].currentValue + '</td>';
            $("#tableDynamic").append($('<tr></tr>').html(itemHTML));
        }
        self.CancelWait();
        self.HeaderN();


    }

    self.onGetItemSucceeded = function () {
        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        $("#listDynamic").empty();
        listRecoder = [];
        itemCollection = [];
        while (itemEnumerator.moveNext()) {
            itemCollection.push(itemEnumerator.get_current());
        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.get_item('Title')).toLowerCase(), nameB = String(b.get_item('Title')).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        for (var count in itemCollection) {
            itemHTML = '<li id="' + itemCollection[count].get_id() + '"><div class="soe-main-list-image"><img src="../Images/Items.png" alt="Items" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_item('Title') + '</div></li>';
            $('#listDynamic').append($('<li></li>').html(itemHTML));
            listRecoder.push(itemHTML);

        }
        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();
    }

    self.onGetItemCollectionSucceeded = function () {
        $("#divMain").hide();
        $("#divDynamic").show();
        itemCollection = [];
        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        $("#listDynamic").empty();
        listRecoder = [];
        while (itemEnumerator.moveNext()) {
            itemCollection.push(itemEnumerator.get_current());

        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.get_title()).toLowerCase(), nameB = String(b.get_title()).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        for (var count in itemCollection) {
            if (listSaver[0] == "APPS") {
                itemHTML = '<li id="' + itemCollection[count].get_title() + '"><div class="soe-main-list-image"><img src="../Images/AppsIcon.png" alt="App" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_title() + '</div></li>';
            }
            else if (listSaver[0] == "SUBSITES") {
                itemHTML = '<li id="' + itemCollection[count].get_title() + '"><div class="soe-main-list-image"><img src="../Images/web.PNG" alt="web" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_title() + '</div></li>';
            }
            else if (listSaver[0] == "FIELDS" || listSaver.length == 3 && listSaver[2] == "Fields") {
                itemHTML = '<li id="' + itemCollection[count].get_title() + '"><div class="soe-main-list-image"><img src="../Images/Fields.PNG" alt="Fields" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_title() + '</div></li>';
            }
            else if (listSaver.length == 3 && listSaver[2] == "Views") {
                itemHTML = '<li id="' + itemCollection[count].get_title() + '"><div class="soe-main-list-image"><img src="../Images/Views.PNG" alt="Views" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_title() + '</div></li>';
            }
            else if (listSaver[listSaver.length - 1] == "GROUPS" || listSaver[listSaver.length - 1] == "Groups") {
                itemHTML = '<li id="' + itemCollection[count].get_id() + '"><div class="soe-main-list-image"><img src="../Images/Groups.PNG" alt="Group" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_title() + '</div></li>';
            }
            else if (listSaver[listSaver.length - 1] == "ALLUSERS" || listSaver[listSaver.length - 1] == "Users") {
                itemHTML = '<li id="' + itemCollection[count].get_id() + '"><div class="soe-main-list-image"><img src="../Images/Users.PNG" alt="User" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_title() + '</div></li>';
            }
            else {
                itemHTML = '<li id="' + itemCollection[count].get_title() + '"><div class="soe-main-list-image"><img src="../Images/Items.png" alt="Items" /></div>' +
                    '<div class="soe-main-list-title">' + itemCollection[count].get_title() + '</div></li>';
            }
            listRecoder.push(itemHTML);
            if (listSaver[0] == 'APPS' && itemCollection[count].get_webTemplate() != 'APP')
                listRecoder.pop();
            else {
                $('#listDynamic').append($('<li></li>').html(itemHTML));
            }
        }
        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();

    };

    self.onGetWebPartSucceeded = function () {
        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        $("#listDynamic").empty();
        ItemWebParts = [];
        listRecoder = [];
        while (itemEnumerator.moveNext()) {
            var NewItem = itemEnumerator.get_current().get_webPart();
            ItemWebParts.push(NewItem);
            self.appWebContext.load(ItemWebParts[ItemWebParts.length - 1]);
        }
        self.appWebContext.executeQueryAsync(self.onLoadWebPart, self.onError);
    }
    self.onLoadWebPart = function () {
        var itemHTML = "";
        ItemWebParts.sort(function (a, b) {
            var nameA = a.get_title().toLowerCase(), nameB = b.get_title().toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        for (var Item in ItemWebParts) {
            itemHTML = '<li id="' + ItemWebParts[Item].get_title() + '"><div class="soe-main-list-image"><img src="../Images/WebParts.PNG" alt="webpart" /></div>' +
            '<div class="soe-main-list-title" >' + ItemWebParts[Item].get_title() + '</div></li>';
            $('#listDynamic').append($('<li></li>').html(itemHTML));
            listRecoder.push(itemHTML);
        }
        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();
    }

    self.onGetEventReceiversSucceeded = function () {

        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        $("#listDynamic").empty();
        listRecoder = [];
        itemCollection = [];
        while (itemEnumerator.moveNext()) {
            itemCollection.push(itemEnumerator.get_current());
        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.get_receiverName()).toLowerCase(), nameB = String(b.get_receiverName()).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        for (var count in itemCollection) {
            itemHTML = '<li id="' + itemCollection[count].get_receiverId() + '"><div class="soe-main-list-image"><img src="../Images/EventReceivers.PNG" alt="EventReceiver" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_receiverName() + '</div></li>';
            $('#listDynamic').append($('<li></li>').html(itemHTML));
            listRecoder.push(itemHTML);
        }
        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();
    };

    self.GetEventReceiversDetails = function () {
        try {
            headerSaver[headerSaver.length - 1] = Item.get_receiverName();
            self.HeaderN();
        }
        catch (err) {
        }
        self.onGetProperties();



    };

    self.LISTS_Click = function () {
        listSaver.push("LISTS");
        headerSaver.push("LISTS");
        self.itemCollection = self.hostWeb.get_lists();
        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetListSucceeded, self.onError);
        self.Wait();

    };

    self.onGetListSucceeded = function () {
        $("#divMain").hide();
        $("#divDynamic").show();
        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        $("#listDynamic").empty();
        listRecoder = [];
        itemCollection = [];
        while (itemEnumerator.moveNext()) {
            itemCollection.push(itemEnumerator.get_current());
        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.get_title()).toLowerCase(), nameB = String(b.get_title()).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        for (var count in itemCollection) {
            if (itemCollection[count].get_baseType() == 0) {
                itemHTML = '<li id="' + itemCollection[count].get_title() + '"><div class="soe-main-list-image"><img src="../Images/ListIcon.png" alt="Lists" /></div>' +
                    '<div class="soe-main-list-title" >' + itemCollection[count].get_title() + '</div></li>';
                $('#listDynamic').append($('<li></li>').html(itemHTML));
                listRecoder.push(itemHTML);
            }
        }
        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();


    };

    self.PROPERTIES_Click = function () {
        $("#divMain").hide();
        $("#divDynamic").show();
        listSaver.push("PROPERTIES");
        headerSaver.push("PROPERTIES");
        self.Wait();
        listRecoder = [];
        $("#listDynamic").empty();
        for (x in propertyDetails) {
            itemHTML = '<li id="' + propertyDetails[x] + '"><div class="soe-main-list-image"><img src="../Images/Properties.png" alt="Properties" /></div>' +
            '<div class="soe-main-list-title" >' + propertyDetails[x] + '</div></li>';
            $('#listDynamic').append($('<li></li>').html(itemHTML));
            listRecoder.push(itemHTML);
        }
        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();
    };


    self.LIBRARIES_Click = function () {
        listSaver.push("LIBRARIES");
        headerSaver.push("LIBRARIES");
        self.libraryCollection = self.hostWeb.get_lists();
        self.appWebContext.load(self.libraryCollection);
        self.appWebContext.executeQueryAsync(self.onGetLibrarySucceeded, self.onError);
        self.Wait();
    };

    self.onGetLibrarySucceeded = function () {
        $("#divMain").hide();
        $("#divDynamic").show();
        var itemEnumerator = self.libraryCollection.getEnumerator();
        var itemHTML = "";
        $("#listDynamic").empty();
        listRecoder = [];
        itemCollection = [];
        while (itemEnumerator.moveNext()) {
            itemCollection.push(itemEnumerator.get_current());
        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.get_title()).toLowerCase(), nameB = String(b.get_title()).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        for (var count in itemCollection) {
            if (itemCollection[count].get_baseType() == 1) {

                itemHTML = '<li id="' + itemCollection[count].get_title() + '"><div class="soe-main-list-image"><img src="../Images/LibraryIcon.png" alt="Library" /></div>' +
                    '<div class="soe-main-list-title">' + itemCollection[count].get_title() + '</div></li>';

                $('#listDynamic').append($('<li></li>').html(itemHTML));
                listRecoder.push(itemHTML);
            }
        }
        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();
    };

    self.SITEPAGES_Click = function () {
        listSaver.push("SITEPAGES");
        headerSaver.push("SITEPAGES");
        self.itemCollection = self.hostWeb.get_lists().getByTitle("Site Pages").getItems(camlQuery);
        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetItemFileDirRef, self.onError);
        self.Wait();
    };

    self.APPS_Click = function () {
        listSaver.push("APPS");
        headerSaver.push("APPS");
        self.itemCollection = self.hostWeb.get_webs();
        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
        self.Wait();

    };

    self.CONTENT_TYPES_Click = function () {

        listSaver.push("CONTENTTYPES");
        headerSaver.push("CONTENTTYPES");
        self.itemCollection = self.hostWeb.get_contentTypes();
        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetContentTypeSucceeded, self.onError);
        self.Wait();

    };

    self.onGetContentTypeSucceeded = function () {
        $("#divMain").hide();
        $("#divDynamic").show();
        var itemEnumerator = self.itemCollection.getEnumerator();
        var itemHTML = "";
        $("#listDynamic").empty();
        listRecoder = [];
        itemCollection = [];
        while (itemEnumerator.moveNext()) {
            itemCollection.push(itemEnumerator.get_current());
        }
        itemCollection.sort(function (a, b) {
            var nameA = String(a.get_name()).toLowerCase(), nameB = String(b.get_name()).toLowerCase()
            if (nameA < nameB)
                return -1
            if (nameA > nameB)
                return 1
            return 0
        });
        for (var count in itemCollection) {
            if (listSaver.length == 3 && listSaver[2] == "WorkflowAssociations") {
                itemHTML = '<li id="' + itemCollection[count].get_id() + '"><div class="soe-main-list-image"><img src="../Images/WorkflowAssociations.png" alt="WorkflowAssociations" /></div>' +
                '<div class="soe-main-list-title">' + itemCollection[count].get_name() + '</div></li>';
            }
            else {
                itemHTML = '<li id="' + itemCollection[count].get_id() + '"><div class="soe-main-list-image"><img src="../Images/ContentTypes.png" alt="ContentTypes" /></div>' +
                    '<div class="soe-main-list-title">' + itemCollection[count].get_name() + '</div></li>';
            }
            $('#listDynamic').append($('<li></li>').html(itemHTML));
            listRecoder.push(itemHTML);
        }
        listRecoderMain.push(listRecoder);
        self.CancelWait();
        self.HeaderN();
    };

    self.GetContentDetails = function () {
        try {
            headerSaver[headerSaver.length - 1] = Item.get_name();
            self.HeaderN();
        }
        catch (err) {
        }
        self.onGetProperties();


    };

    self.FIELDS_Click = function () {
        listSaver.push("FIELDS");
        headerSaver.push("FIELDS");
        self.itemCollection = self.hostWeb.get_fields();

        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
        self.Wait();

    };

    self.SUBSITES_Click = function () {
        listSaver.push("SUBSITES");
        headerSaver.push("SUBSITES");
        self.itemCollection = self.hostWeb.get_webs();
        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
        self.Wait();

    };

    self.GROUPS_Click = function () {
        listSaver.push("GROUPS");
        headerSaver.push("GROUPS");
        self.itemCollection = self.hostWeb.get_siteGroups();
        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
        self.Wait();
    }

    self.ALLUSERS_Click = function () {
        listSaver.push("ALLUSERS");
        headerSaver.push("ALLUSERS");
        self.itemCollection = self.hostWeb.get_siteUsers();
        self.appWebContext.load(self.itemCollection);
        self.appWebContext.executeQueryAsync(self.onGetItemCollectionSucceeded, self.onError);
        self.Wait();
    }

    self.buttonBack_Click = function () {
        self.Wait();
        listSaver.pop();
        headerSaver.pop();
        listRecoderMain.pop();
        if (listSaver.length == 0 && headerSaver.length > 0) {
            $("#divMain").show();
            $("#divDynamic").hide();
        }
        else if (listSaver == 0) {
            $("#divMain").show();
            $("#divDynamic").hide();
            $("#buttonMain")[0].src = "../Images/BackBt_Gray.PNG";
            listSaver = [];
            headerSaver = [];
            listRecoderMain = [];
        }
        else if ($("#tableDynamic").is(':visible')) {
            $("#listDynamic").show();
            $("#listDynamic").empty();
            $("#tableDynamic").empty();
            $("#tableDynamic").hide();
            for (x in listRecoderMain[listRecoderMain.length - 1]) {
                $('#listDynamic').append($('<li></li>').html(listRecoderMain[listRecoderMain.length - 1][x]));
            }
        }
        else {
            $("#listDynamic").empty();
            for (x in listRecoderMain[listRecoderMain.length - 1]) {
                $('#listDynamic').append($('<li></li>').html(listRecoderMain[listRecoderMain.length - 1][x]));
            }

        }
        if (listSaver.length == 1 && (listSaver[0] == 'PROPERTIES' || listSaver[0] == 'APPS')) {
            Item = self.hostWeb.get_allProperties();
            self.appWebContext.load(Item);
            self.appWebContext.executeQueryAsync(self.onNothing, self.onError1);
        }
        if (FileDirRef.length > 0)
            FileDirRef.pop();
        self.CancelWait();
        self.HeaderN();

    };
    self.onNothing = function () {
    }

    self.buttonMain_Click = function () {
        if ($("#buttonMain")[0].nameProp == "BackIcon.png") {
            self.Wait();
            $("#divMain").hide();
            $("#divDynamic").show();
            $("#listDynamic").empty();
            headerSaver.pop();
            listSaver.push(headerSaver[headerSaver.length - 1]);

            for (x in listRecoderMain[listRecoderMain.length - 1]) {
                $('#listDynamic').append($('<li></li>').html(listRecoderMain[listRecoderMain.length - 1][x]));
            }
            self.hostWeb = hostWebSubweb[hostWebSubweb.length - 1];
            hostWebSubweb.pop();
            self.CancelWait();
            self.HeaderN();
        }
    };

    self.onError = function (sender, args) {
        //alert('Request failed. ' + args.get_message() +
        //'\n' + args.get_stackTrace());
        self.CancelWait();
        headerSaver.pop();
        listSaver.pop();

    }
    self.onError1 = function (sender, args) {
        //alert('Request failed. ' + args.get_message() +
        //'\n' + args.get_stackTrace());
        self.CancelWait();

    }

};


function getQueryStringParameter(paramToRetrieve) {
    var params =
        document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}